<?php
// 导入基础类
require_once(dirname(__FILE__).'/base.class.php');
class Category extends Base
{
    function __construct()
    {
        // 调用父类的构造函数
        parent::__construct();
    }

    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }

    // 获取分类列表
    function lists()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        $sql = "select * from categories";
        $sql .= " limit {$start},{$page_num}";
        
        $list = $this->db->query($sql);
        if (!empty($list)) {
            // 将路径处理成绝对路径
            foreach($list as $key=>$item){
                $list[$key]['icon']=path($item['icon']);
            }
            // 获取总记录数
            $sql = "select count(id) as count from categories";
            $res = $this->db->get_one($sql);
            $output['data'] =['list'=> $list,'total'=> $res['count']];
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '获取分类失败';
        }
        echo json_encode($output);
    }


    /*
        添加分类
    */ 
    function add()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($_POST) {
            if (isset($_POST['pid'])) {
                $data['pid'] = $_POST['pid'];
            }
            if (isset($_POST['cate_name'])) {
                $data['cate_name'] = $_POST['cate_name'];
            }
            if (isset($_POST['icon'])) {
                $data['icon'] = $_POST['icon'];
            }
            if (isset($_POST['level'])) {
                $data['level'] = $_POST['level'];
            }
            $data['createtime'] = date('Y-m-d H:i:s');
            $query = arr2str($data);
            $sql = "insert into categories set $query";
            $res = $this->db->query($sql);
            if ($res) {
                // 将分类id返回
                $output['cate_id'] = $res;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '操作失败';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用POST方式提交数据';
        }

        echo json_encode($output);
    }

    /*
        更新分类信息
    */ 

    function update()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        $id = $_GET['id'];
        if ($_POST) {
            if (isset($_POST['pid'])) {
                $data['pid'] = $_POST['pid'];
            }
            if (isset($_POST['cate_name'])) {
                $data['cate_name'] = $_POST['cate_name'];
            }
            if (isset($_POST['icon'])) {
                $data['icon'] = $_POST['icon'];
            }
            if (isset($_POST['level'])) {
                $data['level'] = $_POST['level'];
            }
            $data['createtime'] = date('Y-m-d H:i:s');
            $query = arr2str($data);
            $sql = "update categories set $query where id={$id}";
            $output['sql']=$sql;
            $res = $this->db->query($sql);
            if ($res) {
                // 将分类id返回
                $output['cate_id'] = $res;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '操作失败';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '请使用POST方式提交数据';
        }

        echo json_encode($output);
    }

    /*
        获取分类
     */ 
    function detail()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $sql = "select * from categories where id={$id}";
            $res = $this->db->get_one($sql);
            if ($res) {
                $res['icon']=path($res['icon']);
                $output['data'] = $res;
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }

    /*
        删除分类
    */ 
    function del()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            // 测试数据, 不允许删除
            if($id>6){
                $sql = "delete from categories where id={$id}";
                $res = $this->db->query($sql);
                if (!$res) {
                    $output['errcode'] = 3;
                    $output['msg'] = '删除失败';
                }
            }
           
        } else {
            $output['errcode'] = 2;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }
}
